package com.hfjyz.registration.controller;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.validation.BindException;
import org.springframework.validation.Errors;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.SimpleFormController;
import org.springframework.web.servlet.view.RedirectView;

import com.hfjyz.registration.command.AdminLoginCommand;
import com.hfjyz.registration.command.ManagerListCommand;
import com.hfjyz.registration.contants.ExamContants;
import com.hfjyz.registration.service.ManagerService;

public class ManagerListController extends SimpleFormController {

    private ManagerService managerService;

    /*
     * (non-Javadoc)
     * 
     * @see
     * org.springframework.web.servlet.mvc.AbstractFormController#formBackingObject
     * (javax.servlet.http.HttpServletRequest)
     */
    @Override
    protected Object formBackingObject(HttpServletRequest request)
            throws Exception {
        HttpSession session = request.getSession();
        Object cmd = session.getAttribute(getFormSessionAttributeName(request));
        if (cmd != null) {
            return cmd;
        }
        return super.formBackingObject(request);
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.springframework.web.servlet.mvc.AbstractWizardFormController#
     * referenceData(javax.servlet.http.HttpServletRequest, java.lang.Object,
     * org.springframework.validation.Errors, int)
     */
    @SuppressWarnings("rawtypes")
    @Override
    protected Map referenceData(HttpServletRequest request, Object command,
            Errors errors) throws Exception {
        ManagerListCommand cmd = (ManagerListCommand) command;
        return managerService.initManager(cmd, errors);
    }

    @SuppressWarnings( { "rawtypes", "unchecked" })
    @Override
    protected ModelAndView onSubmit(HttpServletRequest request,
            HttpServletResponse response, Object command, BindException errors)
            throws Exception {
        ManagerListCommand cmd = (ManagerListCommand) command;
        request.getSession().setAttribute(getFormSessionAttributeName(), cmd);
        Map model = errors.getModel();
        if (request.getParameter("btnAdd") != null) {
            return new ModelAndView(new RedirectView("managerEdit.action"),
                    model);
        } else if (request.getParameter("actionEditA") != null) {
            RedirectView rv = new RedirectView("modifyAdmin.action");
            rv.setExposeModelAttributes(true);
            model.put("managerid", cmd.getEid());
            return new ModelAndView(rv, model);
        } else if (request.getParameter("actionDeleteA") != null) {
			AdminLoginCommand adminCmd = (AdminLoginCommand) request
					.getSession().getAttribute("AdminInfo");
			managerService.loggin(request, ExamContants.LOGING_TYPE_INFO,
					(Integer) null, ExamContants.LOGING_USER_TYPE_MANAGE,
					adminCmd.getAdminId(), ExamContants.LOGING_OP_TYPE_DELETE,
					"管理员删除");
            managerService.delete(request, cmd, errors);
        }
        managerService.filter(request, response, cmd, errors);
        return new ModelAndView(getFormView(), model);
    }

    public ManagerService getManagerService() {
        return managerService;
    }

    public void setManagerService(ManagerService managerService) {
        this.managerService = managerService;
    }

}